#################################
#This file creates the data needed for Figure 7 analyses
#################################

library(rio)
library(tidyverse)

#############Reshaping and exporting data for vce(cluster) analyses in Stata

lucid <- import("./Data/lucid_coded_all.rda")

lucid1 <- lucid %>%
  mutate(d_programmatic = ifelse(d_policy ==1, 1, ifelse(d_ideol == 1, 1, 
                                                         ifelse(d_principles, 1, 0))), 
         r_programmatic = ifelse(r_policy ==1, 1, ifelse(r_ideol == 1, 1, 
                                                         ifelse(r_principles, 1, 0))))


####This is a function for cleaning the data x should be "d_x" and y shoud be "r_x""
long_func <- function(x, y) {
  lucid1 %>%
    select(id, 
           rep_extreme_1, dem_extreme_1, too_extreme_1, 
           knowl_mean, opinionation, pid_3, op_ideol_cons, ideol_ext, age, 
           gender, race_eth, house_income, educ, gil_treat, corker_treat, self_ideol, 
           dem_ideol, rep_ideol, pid_idstr_all, x,y) %>%
    mutate(dem_prox = abs(self_ideol - dem_ideol), 
           rep_prox = abs(self_ideol - rep_ideol), 
           ppd = abs(dem_prox - rep_prox)) %>%
    pivot_longer(cols=x:y, 
                 names_to="party", 
                 values_to="outcome") %>%
    filter(!is.na(outcome)) %>%
    mutate(party = factor(party, 
                          levels=c(x, y), 
                          labels=c("Democratic Party", "Republican Party")), 
           too_extreme_1 = factor(too_extreme_1, 
                                  levels=c(2,3), 
                                  labels=c("One Party Too Extreme", "Both Parties Too Extreme")), 
           pid_3 = factor(pid_3, levels=c(1,2,3), 
                          labels=c("Democrat", "Republican", "Independent")), 
           op_ideol_cons = scales::rescale(op_ideol_cons), 
           ideol_ext = factor(ideol_ext, levels=c(1,2,3,4), 
                              labels=c("Moderate", "Slight Lib/Cons", "Mod Lib/Cons", 
                                       "Very Lib/Cons")), 
           age = scales::rescale(age), 
           race1 = car::recode(race_eth, '1=0;2=1;3=1;4=1;5=1'),
           race1 = factor(race1, levels=c(0,1), 
                          labels=c("White", "Non-White")),
           house_income = scales::rescale(house_income), 
           educ = factor(educ, levels=c(1,2,3,4,5), 
                         labels=c("<HS", "HS", "Some College", "BA", "Post-BA"))) 
}


################
#Creating the datasets
#################
programmatic_long <- long_func("d_programmatic", "r_programmatic")
ideol_long <- long_func("d_ideol", "r_ideol")
principle_long <- long_func("d_principles", "r_principles")
policy_long <- long_func("d_policy", "r_policy")
sgroups_long <- long_func("d_sgroups", "r_sgroups")
politicians_long <- long_func("d_politicians", "r_politicians")
traits_long <- long_func("d_traits", "r_traits")
process_long <- long_func("d_process_update", "r_process_update")

#Export
path_out = './Data/'

export(programmatic_long, paste(path_out, 'extreme_programmatic_long.dta', sep=''))
export(ideol_long, paste(path_out, 'extreme_ideol_long.dta', sep=''))
export(principle_long, paste(path_out, 'extreme_principle_long.dta', sep=''))
export(policy_long, paste(path_out, 'extreme_policy_long.dta', sep=''))
export(sgroups_long, paste(path_out, 'extreme_sgroups_long.dta', sep=''))
export(politicians_long, paste(path_out, 'extreme_politicians_long.dta', sep=''))
export(traits_long, paste(path_out, 'extreme_traits_long.dta', sep=''))
export(process_long, paste(path_out, 'extreme_process_long.dta', sep=''))



